Welcome Guest | Sign in | Register

Home > Java Programming > Language Fundamentals > Questions and Answers

01. 1. What will be the output of the program?
public class CommandArgs
{
public static void main(String [] args)
{
String s1 = args[1];
String s2 = args[2];
String s3 = args[3];
String s4 = args[4];
System.out.print(" args[2] = " + s2);
}
}
and the command-line invocation is
> java CommandArgs 1 2 3 4
A. args[2] = 2 B. args[2] = 3
C. args[2] = null D. An exception is thrown at runtime.

Answer and Explanation

Answer: An exception is thrown at runtime.

Explanation:
An exception is thrown because in the code String s4 = args[4];, the array index (the fifth element) is out of bounds. The exception thrown is the cleverly named ArrayIndexOutOfBoundsException.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
02.  What will be the output of the program?

public class TestDogs
{
public static void main(String [] args)
{
Dog [][] theDogs = new Dog[3][];
System.out.println(theDogs[2][0].toString());
}
}
class Dog { }
A. null B. theDogs
C. Compilation fails D. An exception is thrown at runtime

Answer and Explanation

Answer: An exception is thrown at runtime

Explanation:
The second dimension of the array referenced by theDogs has not been initialized. Attempting to access an uninitialized object element (System.out.println(theDogs[2][0].toString());) raises aNullPointerException.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
03. What will be the output of the program?
public class X
{
public static void main(String [] args)
{
String names [] = new String[5];
for (int x=0; x < args.length; x++)
names[x] = args[x];
System.out.println(names[2]);
}
}
and the command line invocation is
> java X a b
A. names B. null
C. Compilation fails D. An exception is thrown at runtime

Answer and Explanation

Answer: null

Explanation:
The names array is initialized with five null elements. Then elements 0 and 1 are assigned the String values "a" and "b" respectively (the command-line arguments passed to main). Elements of names array 2, 3, and 4 remain unassigned, so they have a value of null.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
04. Which one of these lists contains only Java programming language keywords?
A. class, if, void, long, Int, continue B. goto, instanceof, native, finally, default, throws
C. try, virtual, throw, final, volatile, transient D. strictfp, constant, super, implements, do
E. byte, break, assert, switch, include

Answer and Explanation

Answer: goto, instanceof, native, finally, default, throws

Explanation:
All the words in option B are among the 49 Java keywords. Although goto reserved as a keyword in Java, goto is not used and has no function.
Option A is wrong because the keyword for the primitive int starts with a lowercase i.
Option C is wrong because "virtual" is a keyword in C++, but not Java.
Option D is wrong because "constant" is not a keyword. Constants in Java are marked static and final.
Option E is wrong because "include" is a keyword in C, but not in Java.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
05. Which is a valid keyword in java?
A. interface B. string
C. Float D. unsigned

Answer and Explanation

Answer: interface

Explanation:
interface is a valid keyword.
Option B is wrong because although "String" is a class type in Java, "string" is not a keyword.
Option C is wrong because "Float" is a class type. The keyword for the Java primitive is float.
Option D is wrong because "unsigned" is a keyword in C/C++ but not in Java.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
06. You have the following code in a file called Test.java

class Base{
public static void main(String[] args){
System.out.println("Hello");
}
}

public class Test extends Base{}

What will happen if you try to compile and run this?
A. It will fail to compile. B. Runtime error
C. Compiles and runs with no output. D. Compiles and runs printing "Hello"

Answer and Explanation

Answer: Compiles and runs printing "Hello"

Explanation:
This will compile and print "Hello"
The entry point for a standalone java program is
the main method of the class that is being run.
The java runtime system will look for that method
in class Test and find that it does have such a method.
It does not matter whether it is defined in the class itself
or is inherited from a parent class.   

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
07.   What is the result of trying to compile and run the following code.
public final static void main(String[] args){
double d = 10.0 / -0;
if(d == Double.POSITIVE_INFINITY)
System.out.println("Positive infinity");
else
System.out.println("Negative infinity");
}
A. output Positive infinity B. output Negative infinity
C. Will fail to compile D. Runtime exception

Answer and Explanation

Answer: output Positive infinity

Explanation:
There is no such thing as a positive or negative zero.
Hence the result is always positive infinity.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
08.    What is the result that will be printed out ?
void aMethod()
{
float f = (1 / 4) * 10;
int i = Math.round(f);
System.out.println(i);
}
A. 2 B. 0
C. 3 D. 2.5
E. 25

Answer and Explanation

Answer: 0

Explanation:
The result of 1/4 will be zero because integer
divion is carried out on the operands.
If you need to obtain a fractional value
you need to use either a float or double literal
as in 1F / 4F.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
09. Which of the following are valid declarations?

Note : None of the literals used here
contain the character O they are all zeroes.

1. int i = 0XCAFE;
2. boolean b = 0;
3. char c = 'A';
4. byte b = 128;
5. char c = "A";
A. 1,2 B. 1,4
C. 2,4 D. 4,5
E. 1,3

Answer and Explanation

Answer: 1,3

Explanation:
1. is correct as it is a valid hexadecimal number.2. is wrong
because you can only assign the values true and false to them
4 is wrong because 128 is beyond the range of a byte. 5is wrong
because "A" is not a char it is a String. 

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
10.   What is the result of trying to compile and run this program.
public class Test{
public static void main(String[] args){
int[] a = {1};
Test t = new Test();
t.increment(a);
System.out.println(a[a.length - 1]);
}
void increment(int[] i){
i[i.length - 1]++;
}
}
A. Compiler error. B. Compiles and runs printing out 2
C. Compiles and runs printing out 1
D. An ArrayIndexOutOfBounds Exception at runtime

Answer and Explanation

Answer: Compiles and runs printing out 2

Explanation:
 You are passing a reference to an array as
the argument to the method. The method may not
modify the passed object reference but it can modify
the object itself.  

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum



Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved © 2012-2015 SoftLucent.